package com.swanclouds.redfish.account;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.swanclouds.api.account.LdapService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.account.LdapController;
import com.swanclouds.model.account.LdapControllerResource;
import com.swanclouds.model.account.LdapResource;
import com.swanclouds.model.common.ActionResponse;
import com.swanclouds.model.common.Payload;
import com.swanclouds.redfish.account.domain.LdapControllerConcrete;
import com.swanclouds.redfish.account.domain.LdapControllerResourceModel;
import com.swanclouds.redfish.account.domain.LdapResourceModel;
import com.swanclouds.redfish.internal.BaseRedfishService;

import java.util.List;

/**
 * @author 张立强
 * @version jdk1.8.0
 */
public class LdapServiceImpl extends BaseRedfishService implements LdapService {
    /**
     * 查询 Ldap 服务资源
     *
     * @return this ldap service resource
     */
    @Override
    public LdapResource get() {
        return get(LdapResourceModel.class, uri(ClientConstants.ACCOUNTSERVICE_LDAP_SERVICE)).execute();
    }

    /**
     * 查询 Ldap 域控制器集合信息
     *
     * @return Ldap Controller Collection
     */
    @Override
    public LdapControllerResource getLdapControllers() {
        return get(LdapControllerResourceModel.class, uri(ClientConstants.ACCOUNTSERVICE_LDAP_SERVICE_LDAPCONTROLLER)).execute();
    }

    /**
     * 查询具体域控制器的信息
     *
     * @param id this Controller id
     * @return this Controller info
     */
    @Override
    public LdapController getController(String id) {
        Preconditions.checkNotNull(id, "Ldap Controller Id is null");
        return get(LdapControllerConcrete.class, uri(ClientConstants.ACCOUNTSERVICE_LDAP_SERVICE_LDAPCONTROLLER_ID, id)).execute();
    }

    /**
     * 查询具体域控制器列表信息
     *
     * @return this all Controller info
     */
    @Override
    public List<LdapController> list() {
        List<LdapController> ldapControllerList = Lists.newArrayList();
        LdapControllerResource controllers = getLdapControllers();
        controllers.getControllers().stream().forEach(ref -> {
            ldapControllerList.add(get(LdapControllerConcrete.class,uri(ref)).execute());
        });
        return ldapControllerList;
    }

    /**
     * 导入Ldap 证书
     *
     * @param payload cert
     * @param id      Ldap Controller id
     * @return response
     */
    @Override
    public ActionResponse importCert(Payload payload, String id) {
        return null;
    }

    /**
     * 修改具体域控制器的信息
     *
     * @param controller 更新内容
     * @param id             the update Ldap Controller id
     * @return this Ldap Controller info
     */
    @Override
    public LdapController updateController(LdapController controller, String id) {
        Preconditions.checkNotNull(id,"Ldap Controller Id is null");
        Preconditions.checkNotNull(controller, "Controller info is null");
        return patch(LdapControllerConcrete.class, uri(ClientConstants.ACCOUNTSERVICE_LDAP_SERVICE_LDAPCONTROLLER_ID, id)).entity(controller).execute();
    }
}
